package com.dyh.quickdemo.spel;

import com.alibaba.fastjson.JSONObject;
import com.dyh.quickdemo.injectMap.BizHandlerHQ;
import lombok.Getter;
import lombok.Setter;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.Properties;

/*
@Author=Joey Wheeler
@Date=2021/5/19 11:14
*/
@SpringBootTest
public class TestSpel2 {

    @Autowired
    BizHandlerHQ bizHandlerHQ;

    @Setter@Getter
    class Context{
        String name;
        int age;
    }



    @Test
    public void test(){
        SpelExpressionParser spelParser = new SpelExpressionParser();
        EvaluationContext context = new StandardEvaluationContext();

        bizHandlerHQ.getHandlerMap().forEach(context::setVariable);


        Context ctx = null;
//        ctx = new Context();
//        ctx.setAge(24);
//        ctx.setName("yjsnp");

        context.setVariable("ctx",ctx);

        Properties properties  = new Properties();
        properties.setProperty("fox1","kei");
        properties.setProperty("fox2","meiya");
        properties.setProperty("fox3","takeru");


        context.setVariable("properties",properties);

        /*?.val 预防空指针*/
        /*properties 必须是.getProperty*/
        Expression expression = spelParser.parseExpression(
                        "#type97SendBizHandler.handle(1,'hello',#ctx?.name,#properties?.getProperty('fox1')"
        );

        Object value = expression.getValue(context);

    }


}
